package ex01;

import java.io.*;
import java.net.Socket;

/**
 * Created by lili on 2017/8/7
 */
public class TestSocket {

    private static final String IP = "127.0.0.1";
    private static final int PORT = 8765;

    public static void main(String[] args) throws Exception {

        Socket socket = new Socket(IP,PORT);
        OutputStream os = socket.getOutputStream();
        boolean autoflush = false;
        PrintWriter out = new PrintWriter(socket.getOutputStream(),autoflush);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.println("GET /index.jsp HTTP/1.1");
        out.println("Host: localhost:8080");
        out.println("Connection: Close");
        out.println();
        boolean loop = true;
        StringBuffer sb = new StringBuffer(8096);
        while (loop){
            if(in.ready()){
                int i = 0;
                while(i!=-1){
                    i = in.read();
                    sb.append((char)i);
                }
                loop = false;
            }
            Thread.currentThread().sleep(50);
        }

        System.out.println(sb.toString());
        socket.close();

    }

}
